package cn.qiyu5522.security;

import cn.qiyu5522.domain.entity.ResponseResult;
import cn.qiyu5522.enums.AppHttpCodeEnum;
import cn.qiyu5522.utils.WebUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClassName: AccessDeniedHandlerImpl
 * Package: cn.qiyu5522.handlers.security
 * Description:  授权失败处理器
 *
 * @Author: Tonkey
 * @Create：2024/4/12-15:11
 * @Version: v1.0
 */

@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {

    @Resource
    private ObjectMapper objectMapper;

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        accessDeniedException.printStackTrace();
//
        ResponseResult responseResult = ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        WebUtils.renderString(response, objectMapper.writeValueAsString(responseResult));
    }
}
